package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunBossConfig;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.entity.enums.activity.horserun.eHorseRunAttrEnum;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.manger.activity.horserun.HorseRunMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

public class HorseRunBossBattleEvent extends HorseRunBaseEvent{

    @Override
    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config, HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        builder.setError(0);
        int eventId = eventConfigById.getId();

        int randomAppearance = config.randomBattleHorseAppearance();
        userGamingData.setBattleHorseId(randomAppearance);

        List<Integer> logParam = new ArrayList<>();
        logParam.add(randomAppearance);

        //加到日志里面
        addUserLog(userGamingData,config,1,eventId,logParam,builder);

        eventGoing(userGamingData);
        return builder;
    }


    @Override
    public HorseRunProto.EventResult.Builder deal(HorseRunUserGamingData userGamingData, HorseRunDealEventArg arg, HorseRunConfig config, HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();

        HorseRunBossConfig thisProcessBossConfig = config.getThisProcessBossConfig(userGamingData.getProcessId());
        builder.setError(0);
        //拿不到boss 直接结束吧
        if (thisProcessBossConfig == null){

//            logger.error("horse run activityId{} boss config error event reset userId{}",userGamingData.getActivityId(),userGamingData.getUserId());
            //洗白
//            userGamingData.setEventId(0);
            //事件完成
//            eventNoDeal(userGamingData);
            //不该生成这个事件
//            builder.setError(GameErrorCode.E_HORSE_RUN_CONFIG_ERROR);
            //拿不到boss 死
            userGamingData.setNowConfident(0);
            return builder;
        }

        List<Integer> floatList = new ArrayList<>();
        if (userGamingData.getBackGround() == 1){
            floatList = new ArrayList<>(thisProcessBossConfig.getNormalLevel());
        }else if (userGamingData.getBackGround() == 2){
            floatList = new ArrayList<>(thisProcessBossConfig.getMidLevel());
        }else if (userGamingData.getBackGround() == 3){
            floatList = new ArrayList<>(thisProcessBossConfig.getHighLevel());
        }else {
            logger.error("horse run activityId{} boss config error event reset userId{}",userGamingData.getActivityId(),userGamingData.getUserId());
//            洗白
            userGamingData.setEventId(0);
//            事件完成
            eventNoDeal(userGamingData);
//            不该生成这个事件
            builder.setError(GameErrorCode.E_HORSE_RUN_CONFIG_ERROR);
            return builder;
        }

        int process = userGamingData.getProcessId()+1;
        Random random = HorseRunMgr.getRandom();
        int randomProcess =0;
        if (floatList.get(1).equals(floatList.get(0)) ){
            randomProcess = floatList.get(0);
        }else {
            randomProcess = random.nextInt(floatList.get(1) - floatList.get(0) + 1) + floatList.get(0);
        }
        if (randomProcess>0){
            process = randomProcess;
        }
        HorseRunBossConfig lastProcessBossConfig = config.getLastProcessBossConfig(process);

        if (lastProcessBossConfig == null){
            logger.error("horse run activityId{} boss config error event reset userId{}",userGamingData.getActivityId(),userGamingData.getUserId());
//            洗白
            userGamingData.setEventId(0);
//            事件完成
            eventNoDeal(userGamingData);
//            不该生成这个事件
            builder.setError(GameErrorCode.E_HORSE_RUN_CONFIG_ERROR);
            //死
            userGamingData.setNowConfident(0);
            return builder;
        }


        Map<Integer, Integer> bossAttrMap = lastProcessBossConfig.getAttrMap();

        long bossGrade = config.getGradeByAttr(bossAttrMap);

        HorseRunProto.HorseRunBattleResult.Builder battleBuild = HorseRunProto.HorseRunBattleResult.newBuilder();
        battleBuild.setEnemyAppearance(userGamingData.getBattleHorseId());
        battleBuild.addEnemyAttr(bossAttrMap.getOrDefault(eHorseRunAttrEnum.energy.getValue(),0));
        battleBuild.addEnemyAttr(bossAttrMap.getOrDefault(eHorseRunAttrEnum.artful.getValue(),0));
        battleBuild.addEnemyAttr(bossAttrMap.getOrDefault(eHorseRunAttrEnum.resistance.getValue(),0));
        battleBuild.setEnemyGrade(bossGrade);

        battleBuild.addMyAttr(userGamingData.getGamingTotalAttr().getOrDefault(eHorseRunAttrEnum.energy.getValue(),0));
        battleBuild.addMyAttr(userGamingData.getGamingTotalAttr().getOrDefault(eHorseRunAttrEnum.artful.getValue(),0));
        battleBuild.addMyAttr(userGamingData.getGamingTotalAttr().getOrDefault(eHorseRunAttrEnum.resistance.getValue(),0));

        battleBuild.setMyGrade(userGamingData.getGamingGrade());

        List<Integer> logParam = new ArrayList<>();
        //第一个参数是对方小马id
        logParam.add(userGamingData.getBattleHorseId());

        if (userGamingData.getGamingGrade() > bossGrade){
            battleBuild.setIsWin(true);
            //胜利
            logParam.add(1);
            int exp = thisProcessBossConfig.getExp() * userGamingData.getOpenGameMultiple();
            logParam.add(exp);

            userGamingData.addExpSum(exp);

            builder.setExpAdd(exp);
        }else {
            battleBuild.setIsWin(false);
            //失败
            logParam.add(0);
            //扣血
            logParam.add(-1);

            userGamingData.reduceConfident(1);
            builder.setAddConfident(-1);
        }
        builder.setBattle(battleBuild);

        eventDone(userGamingData);
        addUserLog(userGamingData,config,2,eventId,logParam,builder);
        return builder;
    }
}
